#ifndef QCPICKRESULTVIEW_H
#define QCPICKRESULTVIEW_H

#include <QFrame>

class QcGroupedListView;
class QTreeView;
class QcPickResultListModel;
class QcPickResultTreeModel;

// 整合list和icon两种显示模式
class QcPickResultView : public QFrame
{
    Q_OBJECT
public:
    explicit QcPickResultView(QWidget *parent = nullptr);

    enum KeViewMode
    {
        k_view_none,
        k_view_list,
        k_view_small_icon,
        k_view_median_icon,
        k_view_large_icon
    };

    KeViewMode viewMode() const { return viewMode_; }
    void setViewMode(KeViewMode mode);

    void reset();

    int groupCount() const;

    // 返回分组@group包含的子项数目
    // 若group=-1, 返回无分组状态下的项目总数
    int groupSize(int group) const;

    // 统计项目总数
    int itemAmount() const;

    // 在分组@group中添加扫描结果@filePath. group为分组序号，-1表示不分组
    void appendItem(int group, const QString& filePath);

    void setItemChecked(int group, int item, bool checked);

    bool isItemChecked(int group, int item) const;

    void deleteItem(int group, int item);

    void deleteGroup(int group);

    bool isListRowExpanded(int listRow) const;

    void setListRowExpanded(int listRow, bool expanded);

    bool isListRowVisible(int listRow) const;

    void showView(bool show);

    // @groupRemovalSize: 决定如何执行删除操作.
    //   当>=0时，回调fn之后将删除checkedItem，否则不删除item;
    //   当>=groupSize时，还将删除当前分组
    void forEachCheckedItem(std::function<void(const QString&)> fn, int groupRemovalSize);

    void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);

    int sortColumn() const;

    Qt::SortOrder sortOrder() const;
    void setSortOrder(Qt::SortOrder order);

    int columnCount() const;
    QString columnName(int col) const;

    // 显示缩略图还是文件icon
    bool useThumbnail() const;
    void setUseThumbnail(bool b);

    bool showDisplayRole() const { return showText_; }
    void setShowDisplayRole(bool b);

protected:
    void resizeEvent(QResizeEvent *event) override;

signals:
    void checkedFilesChanged(int checkedFiles, qint64 totalSize);

private slots:

    // 该方法用来更新checkedItemsCount_和checkedItemsSize_成员
    void on_checkState_changed(const QModelIndex &topLeft, const QModelIndex &bottomRight);

private:
    KeViewMode viewMode_{ k_view_none };

    QcGroupedListView* listView_;
    QTreeView* treeView_;
    QcPickResultListModel* listModel_;
    QcPickResultTreeModel* treeModel_;

    // 用来追踪被选中的项目数量和大小
    int checkedItemsCount_{ 0 };
    qint64 checkedItemsSize_{ 0 };

    bool showText_{ false }; // 仅在icon模式下有效，用来决定是否显示文字
};

#endif // QCPICKRESULTVIEW_H
